import dd from 'gdt-jsapi';
const baseUrl = 'https://wrj.daishan.gov.cn/gate/nd';

const fetchData = (url, method, data) => {
  // console.log('fetchData', url, data)
  return new Promise((resolve, reject) => {
    try {
      // console.log('8888888888', dd)
      const _method = method.toUpperCase()
      let _data = null
      if (_method === 'POST' && data) {
        try {
          _data = JSON.stringify(data)
        } catch(e){}
      }
      dd.httpRequest({
        headers: {
          "Content-Type": "application/json",
          token: 'e44579a7c8df1a0d4ad5841978239e68'
        },
        url: baseUrl + url,
        method: _method,
        // 需要手动调用JSON.stringify将数据进行序列化
        data: _data,
        dataType: "json",
      }).then(res => {
        // console.log('fetch result', res)
        if (res.status === 200 && res.data) {
          resolve(res.data)
        } else {
          reject(res)
        }
      }).catch(err => {
        // console.log('fetch Error', err)
        reject(err)
      })
    } catch(e){
      console.log(e)
      reject(e)
    }

  })
}
export default {
  getUserInfo: (authCode) => fetchData('/ding-auth/getuser', 'post', {authCode}),
  getDeviceList: (status) => fetchData(`/apply-admin/device/list?status=${status}`, 'get'),
  apply: (data) => fetchData('/apply-admin/apply/add', 'post', data),
  cancelApply: (id) => fetchData('/apply-admin/apply/cancel', 'post', {id}),
  getApplyCategoryList: () => fetchData('/apply-admin/category/list', 'get'),
  getDevicePlan: (deviceSn) => fetchData(`/apply-admin/apply/device-plan?deviceSn=${deviceSn}`, 'get'),
  getApplyList: ({pageIndex, pageSize, applyAccountId, status}) => fetchData(`/apply-admin/apply/my-list`, 'post', {pageIndex, pageSize, applyAccountId, status}),
  getPendingMyApproveApplyList: (nextApproveAccountId) => fetchData(`/apply-admin/apply/pd-approve-list`, 'post', {nextApproveAccountId}),
  getApplyDetail: (id) => fetchData(`/apply-admin/apply/detail?id=${id}`, 'get'),
  getPersonList: () => fetchData(`/apply-admin/user/list`, 'get'),
  approve: (data) => fetchData(`/apply-admin/approve/add`, 'post', data),
  getMyApproveList: (approveAccountId) => fetchData(`/apply-admin/approve/list-for-approve?approveAccountId=${approveAccountId}`, 'get'),
}
